package com.start_eternity.oasissafewalk.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.start_eternity.oasissafewalk.mapper.AccountMapper;
import com.start_eternity.oasissafewalk.pojo.*;
import com.start_eternity.oasissafewalk.service.AccountService;
import com.start_eternity.oasissafewalk.service.LoggService;
import com.start_eternity.oasissafewalk.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private LoggService loggService;
    @Autowired
    private HttpServletRequest request;

    public PageBean page() {
        return null;
    }
    @Override
    public PageBean page(Integer page,Integer pageSize, String username, String password, String name,
                         Integer gender, String accountPic, String number, LocalDate begin,
                         LocalDate end) {
        //用logg记录
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());//记录执行的当前时间
        //获取请求头中的jwt令牌, 解析令牌
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name1 = (String) claims.get("username");
        logg.setLoggName(name1);//记录执行者的名字
        logg.setLoggDescription("执行了查询管理员的操作");//记录执行的具体操作
        loggService.insert(logg);//调用loggService接口将logg类的信息记录到数据库里面

        //1.设置分页参数
        PageHelper.startPage(page,pageSize);

        //2.执行查询
        List<Account> accountList = accountMapper.list(username,password,name,gender,accountPic,number,begin, end);
        Page<Account> p=(Page<Account>)accountList;
        //3.封装PageBean对象
        return new PageBean(p.getTotal(),p.getResult());
    }

    @Override
    public Account getByName(String username) {
        return accountMapper.getByName(username);
    }

    @Override
    @Transactional
    public void update(Account account) {
        account.setUpdateTime(LocalDateTime.now());
        accountMapper.update(account);
    }
}
